package cn.iocoder.yudao.module.mp.service.handler.other;

import cn.iocoder.yudao.module.mp.framework.mp.core.context.MpContextHolder;
import cn.iocoder.yudao.module.mp.service.message.MpAutoReplyService;
import cn.iocoder.yudao.module.mp.service.qr.MpQrService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 扫码的事件处理器
 */
@Component
@Slf4j
public class ScanHandler implements WxMpMessageHandler {

    @Resource
    private MpQrService mpQrService;

    @Resource
    private MpAutoReplyService mpAutoReplyService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
                                    WxMpService weixinService, WxSessionManager wxSessionManager) throws WxErrorException {
        log.info("[handle][粉丝{}扫码]", wxMessage.getFromUser());
        mpQrService.handleQrScan(wxMessage.getTicket(), wxMessage.getFromUser());
        return mpAutoReplyService.replyForScan(MpContextHolder.getAppId(), wxMessage.getEventKey(), wxMessage);
    }

}
